<% title_tag @topic.title %>
<% content_for :scripts do %>
  <script type="text/javascript">
    Topics.topic_id = <%= @topic.id %>;
    <% if !@user_like_reply_ids.blank? %>
    Topics.user_liked_reply_ids = [<%= @user_like_reply_ids.join(",") %>];
    <% end %>
    $(document).ready(function(){
      <% if admin? %>
        $("#replies .reply a.edit").css('display','inline-block');
        $("#replies .reply a.suggest").css('display','inline-block');
      <% elsif current_user && current_user.id == @topic.user_id%>
        $("#replies .reply a.suggest[data-uid='<%= current_user.id %>']").css('display','inline-block');
      <% elsif current_user %>
        $("#replies .reply a.edit[data-uid='<%= current_user.id %>']").css('display','inline-block');
      <% end %>

      <% if @topic.closed? %>
        $("#replies .reply .hideable").remove();
      <% end %>
      $.post("<%= read_topic_path(@topic) %>");
    })
  </script>
<% end %>
<%if @topic.audit_status == "pending" && @topic.user_id == current_user&.id %>
  <div class="alert alert-info">
    <%= "文章暂未审核， 仅自己可见， 审核通过后全体可见" %>
  </div>
<%end %>

<% if current_user&.admin?%>
  <div class="alert alert-info">
    <%= "管理员提示：此时帖子审核状态为 #{audit_chinese_status @topic.audit_status}" %>
    <%= link_to "去审核", [:topics, :admin, :audits, id_eq: @topic.id], class: "tw-ml-6 tw-font-bold"%>
  </div>
<%end%>

<div class="row">
  <div class="col-lg-9">
    <div class="topic-detail card">
      <%= render partial: "topics/topic_info", locals: { topic: @topic } %>
      <% if @topic.excellent? %>
        <div class="label-awesome">
          <i class="fa fa-award awesome"></i> 本帖已被设为精华帖！
          <% if can?(:close, @topic) %>
            <div class="pull-right">
              <%= link_to icon_tag("close"), action_topic_path(@topic.id, type: 'normal'), method: :post, remote: true %>
            </div>
          <% end %>
        </div>
      <% end %>
      <div class="card-body markdown markdown-toc">
        <%= raw Setting.before_topic_html %>
        <% if @topic.draft %>
          <div class="text-center alert alert-warning">
            当前状态为 <i class="fas fa-file"></i> 草稿，仅作者可见，请前往 <%= link_to "", edit_topic_path(@topic), class: "fa fa-edit", title: "编辑发布" %> 编辑发布</a>
          </div>
        <%elsif @topic.audit_status != 'approved'%>
          <div class="text-center alert alert-warning">
            当前状态：<%= audit_chinese_status(@topic.audit_status)%>，其他人不可见
            <%if can?(:manage, @topic) %>
              <%= link_to raw("<i class='fa fa-check'></i> 审核通过"), action_topic_path(@topic, type: 'audit_pass'), method: 'post', remote: true, title: "审核通过" %>
              <%= link_to raw("<i class='fa fa-ban'></i> 审核拒绝"), action_topic_path(@topic, type: 'audit_reject'), method: 'post', remote: true, title: "审核拒绝" %>
            <%end %>
          </div>
        <%end %>

        <%= @topic.body_html %>
        <%= raw Setting.after_topic_html %>
      </div>

      <% if not @topic.appends.blank? %>
        <%= render partial: '/topics/append_content', collection:  @topic.appends  %>
      <% end %>

      <% if @topic.cannot_be_shared? %>
        <div class="label-legal">
          <i class="fa fa-legal"></i>「<%= t("topics.cannot_be_shared")%>」
        </div>
      <% end %>

      <div class="clearfix card-footer">
        <%= render "buttons" %>
      </div>
    </div>
    <%= render partial: "ban_reason" %>

    <% if @topic.user&.reward_enabled? %>
      <%= render partial: "shared/reward", object: @topic %>
    <%end%>
    <% if @replies.blank? %>
      <div class="no-result">
        <%= t("topics.no_replies") %>
      </div>
    <% else %>
      <!--先显示最佳回复-->
      <% if !@suggest_replies.blank? %>
        <div id="best-replies" class="card" data-last-floor="<%= @replies.count(:all) %>">
          <div class="best-reply card-header hide-ios">
            <i class="fa fa-check check-green" aria-hidden="true"> </i> 最佳回复
          </div>

          <div class="card-body">
            <%= render partial: "/replies/reply", collection: @suggest_replies %>
          </div>
        </div>
      <% end %>

      <div id="replies" class="card" data-last-floor="<%= @replies.count(:all) %>">
        <%= render partial: "/replies/reply_total", locals: { resource: @topic } %>
        <div class="items card-body">
          <%= render partial: "/replies/reply", collection: @replies %>
        </div>
      </div>
    <% end %>
    <% if current_user %>
      <% if @topic.closed? %>
        <div class="alert alert-info">此话题已经于 <%= l @topic.closed_at, format: :long %> 关闭，不再接受任何回帖。</div>
      <% else %>
        <div id="reply" class="card">
          <div class="card-body">
            <div class="card-title hide-ios">发起回帖</div>
            <% if can? :create, Reply %>
              <%= render 'reply_form' %>
            <% else %>
              <div class="alert alert-info">
              <% if current_user.newbie? %>
                当前设置新手或未审核用户不能在 <%= Setting.night_curfew_start.to_i %>:00 ~ <%= Setting.night_curfew_end.to_i %>:00 发帖。
                <% else %>
                当前宵禁，不能在 <%= Setting.night_curfew_start.to_i %>:00 ~ <%= Setting.night_curfew_end.to_i %>:00 发帖。
                <% end %>
              </div>
            <% end %>
          </div>
        </div>
      <% end %>
    <% else %>
      <div class="card">
        <div class="card-body">
          <%= render partial: "topics/translation/need_login_to_reply" %>
        </div>
      </div>
    <% end %>
  </div>

  <% if !mobile? %>
    <div class="sidebar hidden-mobile col-lg-3">
      <%= render 'topic_sidebar' %>
    </div>
  <% else %>
    <div class="move-page-buttons">
      <a href="#" class="btn btn-block btn-sm btn-move-page" data-type="top"><i class="fa fa-arrow-up"></i></a>
      <a href="#" class="btn btn-block btn-sm btn-move-page" data-type="bottom"><i class="fa fa-arrow-down"></i></a>
    </div>
  <% end %>
</div>
